package com.cn.jx.common.util.docm;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;

import com.jx.model.PropertyEntity;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月1日
 * <p>功能描述：
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
public class ClassInfoUtil {

	/**
	 * 
	* @Title:getClassProperty
	* @Description:TODO(这里用一句话描述这个方法的作用)
	* <p>邮箱地址: 524577360@qq.com
	* @param cls
	* @return
	* List<PropertyEntity>
	* @throws
	 */
	public static List<PropertyEntity> getClassProperty(Class<?> cls,String title){
		List<PropertyEntity> listRole=new ArrayList<PropertyEntity>();
		RequestMapping rmping=cls.getDeclaredAnnotation(RequestMapping.class);
		String urlhead=getRequestMapping(rmping);//url头
		Method[] roleMethod=cls.getDeclaredMethods();
		for(Method method:roleMethod){
			RequestMapping rmp=method.getDeclaredAnnotation(RequestMapping.class);
			String urlend=getRequestMapping(rmp);
			if(!"".equals(urlend)){
				String url=urlhead+urlend; //url地址
				Type type = method.getGenericReturnType();//返回类型
				Parameter[] pmers=method.getParameters();
				String paramval=getParameType(pmers);
				listRole.add(PropertyEntity.getInstance(url, getReturnTypeName(type), paramval, getDescByUrl(urlend,title)));
			}
		}
		return listRole;
	}
	private static String getReturnTypeName(Type type){
		if(type!=null){
			String tname=type.getTypeName();
			if(Utility.isNotBlank(tname)){
				if(tname.indexOf("<")!=-1){
					String txt=tname.substring(tname.lastIndexOf("<"), tname.lastIndexOf(">")+1);
					tname=tname.replace(txt, "");
				}
				String[] snames=tname.split("\\.");
				return snames[snames.length-1];
			}
		}
		return "";
	}
	
	private static String getRequestMapping(RequestMapping rmping){
		if(rmping==null){
			return "";
		}
		String[] vals=rmping.value();
		if(vals.length==0){
			return "";
		}
		String urlhead=vals[0];//url头
		return urlhead;
	}
	
	private static boolean hasStr(String str){
		String par="Model,HttpServletRequest,HttpServletResponse";
		return par.contains(str);
	}
	
	private static String getParameType(Parameter[] pmers){
		String paramval="";
		for(Parameter p:pmers){
			Type tp=p.getParameterizedType();
			String typename=getReturnTypeName(tp);
			if(!hasStr(typename)){
				String val=typename+" "+p.getName();
				if("".equals(paramval)){
					paramval=val;
				}else{
					paramval=paramval+"<br>"+val;
				}
			}
		}
		if("".equals(paramval)){
			paramval="none";
		}
		return paramval;
	}
	
	private static String getDescByUrl(String url,String title){
		String conten=getContent(url);
		return MessageFormat.format(conten, title);
	}
	
	private static String getContent(String tag){
		System.out.println(tag+"***8");
		switch (tag) {
		case "list/{path}":
			return "跳转到{0}列表页面";
		case "edit":
			return "跳转到{0}编辑页面";
		case "recycle":
			return "跳转到{0}回收站页面";
		case "allotResource":
			return "跳转到角色分配页面";
		case "resource/listTree":
			return "角色分配资源的时候弹出资源列表树";
		case "resource/relation":
			return "给角色分配资源关系";
		case "get":
			return "获取{0}对象根据id";
		case "listData/{path}":
			return "返回{0}列表信息";
		case "addOrUpdate":
			return "保存或更新{0}信息;判断参数id是否为空,空则保存否则更新";
		case "delete":
			return "批量删除{0}到回收站;页面id集合,逗号隔开 例如ids=1,2,3";
		case "recover":
			return "恢复回收站内容到{0};页面id集合,逗号隔开 例如ids=1,2,3";
		case "remove":
			return "从{0}回收站彻底移除;页面id集合,逗号隔开 例如ids=1,2,3";
		case "removeAll":
			return "清空回收站({0})";
		case "listTree":
			return "{0}树导航列表";
		case "dict/list":
			return "{0}字典列表";
		default:
			break;
		}
		return "";
	}
}
